<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/icon.css" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/ainit.css" type="text/css"></link>
 	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
 	<script type="text/javascript" src="${pageContext.request.contextPath}/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/homepages/js/functions.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.json-2.4.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.1.2.2.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.ainit-1.0.0.js"></script>
	<script type="text/javascript">
		var a = document.documentElement.clientHeight - 79;
		var b = document.documentElement.clientWidth - 25;
		//选题ID
		var xtid;
		$(function(){
			xtid = '';
			a = document.documentElement.clientHeight - 79;
		 	b = document.documentElement.clientWidth - 25;
			//加载部分字段
			loadPages();
			//去请求按钮
			loadButns();
			//请求datagrid
			loadDategrid();
			saveXT();
			//showPlan1();
			//加载对照信息
			loadDzSource();
		});
		var flowNodeArr;
		var execState;
		
		function loadDzSource(){
			$.ajax({
				url:'${pageContext.request.contextPath}/dz/queryDzList',
				type:'post',
				data:{dzIds:'flowNodes;execState'},
				success:function(data){
					flowNodeArr = data[0];
					execState = data[1];
					showPlan1();//环节点窗口
				}
			});
		}
		
		function flowNodeFormatter(value){
			for(var i=0; i<flowNodeArr.length; i++){
				if (flowNodeArr[i].ID == value) return flowNodeArr[i].NAME;
			}
			return value;
		}
		
		function stateFormatter(value){
			if(value=='')return '未执行';
			for(var i=0; i<execState.length; i++){
				if (execState[i].ID == value) return execState[i].NAME;
			}
			return value;
		}
		
		
		//加载下拉框
		function loadSelect(){
			alert("OK");
		}
		
		var divName="";
		//部分查询框
		function loadPages(){
			$.ajax({
				type:'post',
				url:'${pageContext.request.contextPath}/framework/columnList',
				data:{'colId':'bj_xt','flag':'T'},
				success:function(data){
					if(data ==""){
						alert("数据为空");
					}else{
						//请求一个组织列的方法
						divName='COLUMNS';
						makeColumns(data,divName);
					}
				}
			});
		}
		
		function loadButns(){
			$.ajax({
				type:'post',
				url:'${pageContext.request.contextPath}/framework/buttonsList',
				data:{'butnId':'XTButton'},
				success:function(data){
					if(data == ""){
						alert("按钮数据为空");
					}else{
						//请求一个组织按钮的方法
						var buttons = data;
						var butHTML = "";
						for(var i = 0; i < buttons.length; i++){
							butHTML += formatButtons(buttons[i]);
						}
						$('#BUTTONS').html(butHTML);
						$.parser.parse();  //重新编译界面
					}
				}
			})
		}
		
		function loadDategrid(){
			$.ajax({
				type:'post',
				url:'${pageContext.request.contextPath}/framework/listQueryJson',
				data:{'dsId':'bj_xt','colId':'bj_xt','execFlag':'','flag':''},
				success:function(data){
					if(data==""){
						alert("数据为空");
					}else{
						//给datagrid赋列值，还有rows
						mdatagrid(data);
					}
				}
			});
		}
		
		function  showPlan(){
			var id = $('#doubleId').val();
			if(id == ''){
				$.message.alert('提示','请选择一个选题','info');
				return;
			}
			$('#showPlanW').dialog('open');
			if($('#showPlanTwo').html() == ''){
				$('#showPlanTwo').load("${pageContext.request.contextPath}/bjTopic/afferentParam?id="+id);
			}
		}
		
		function showPlan1(){
			$('#showPlanW').dialog({
				fit:true,
				collapsible: true,
				minimizable: true,
				maximizable: true,
				title:'选题计划',
				maximized:true
			});
			$('#showPlanW').dialog('close');
		}
		
		function mdatagrid(data){ //传入的是queryJson 里面就有columns和rows
			var lastIndex;
			$('#DATALIST').datagrid({
				iconCls:'icon-save',
				height:a,
				width:b,
				nowrap: false,
				striped: true,				 
				collapsible:true,
				autoRowHeight: false,		
				sortOrder: 'desc',
				remoteSort: false,
				fitColumns:true,
				columns:[[]],
				pagination:true,
				rownumbers:true,
				singleSelect:true,
				onDblClickRow:function(rowIndex,rowData){
					showUpdWin();
					xtid = rowData.XT_ID;
					$.ajax({
						type:'post',
						url:'${pageContext.request.contextPath}/bjTopic/getTopic',
						data:{'id':rowData.XT_ID},
						success:function(data){
							InitData('applyTopic',data);
							$('#doubleId').val(rowData.XT_ID);
						}
					})
				}
			});
			var dataGrid = $('#DATALIST');
			dataGrid.datagrid(jointColumn(data.columns));//根据配置选项，生成datagrid  
			//alert(data.rows.length);
			//加载shuju
			$.ajax({
				url:'${pageContext.request.contextPath}/framework/reloadData',
				data:{dsId:'bj_xt'},
				success:function(queryJson){
					$('#DATALIST').datagrid("loadData", queryJson);
					$('#DATALIST').datagrid({url:queryJson.url});
				}
			});
		}
		function InitData(formId,data){
			$("#"+formId+" input").each(function(){
				for(var i in data){
					if($(this).attr('name') == i){
						$(this).val(data[i]);
					}
				}
			});
		} 
		
		function saveTopic(){
			$.ajax({
				type:'post',
				url:'${pageContext.request.contextPath}/bjTopic/saveTopic',
				data:$('#INFO_FORM').serialize(),
				success:function(data){
					if(data == 1){
						alert("添加成功");
						$('#applyTopic').close();
						loadPages();
					}else{
						alert("添加失败"); 
					}
				}
			})
		}
		
		function showUpdWin(){
			$('#makeColumn input').each(function(){
				if($(this).attr('nowrite') == 'true'){
					$(this).attr('readonly',true);
				}
			});
			$('#applyTopic').dialog('open');
		}
 		function showAddWin(){
 			$('#makeColumn input').each(function(){
				if($(this).attr('nowrite') == 'true'){
					$(this).removeAttr('readonly');
				}
			});
 			$('#applyTopic').dialog('open');
 		}
		function saveXT(){
			$.ajax({
				type:'post',
				url:'${pageContext.request.contextPath}/framework/singlePage',
				data:{'colId':'bj_xt','flag':'S','butnId':'saveButn'},
				success:function(data){
					if(data ==""){
						alert("数据为空");
					}else{
					 	divName='makeColumn';
						makeColumns(data.columns,divName);
						var buttons = data.buttons;
						var butHTML = "";
						for(var i = 0; i < buttons.length; i++){
							butHTML += formatButtons(buttons[i]);
						}
						$('#makeButton').html(butHTML);
						$.parser.parse();  //重新编译界面
					}
				}
			});
			$('#applyTopic').dialog({
				width:850,
				height:300,
				collapsible: true,
				minimizable: true,
				maximizable: true,
				title:'添加选题',
				left:150,
				top:90
			});
			$('#applyTopic').dialog('close');
			
		}
//------------------------------------------------------------------------------------------------------------------------------
		//当保存成功后   保存按钮置灰   将另外的按钮（如：审批）拼接到保存后面（暂时还没用到）
		function findOtherButn(){
			var formId= $('#formId').val();
			$.ajax({
				 type:'post',
				 url:'${pageContext.request.contextPath}/topic/specialButn',
				 data:{'formType':formId,'name':'applyTopics'},
				 success:function(da){
				 	//拼接到保存按钮后面
				 	var butns= $('#butn');
				 	for(var i=0;i<da.length;i++){
				 		butns.append("<input type='button' value="+da[i].butnName+" onclick="+"'"+da[i].scriptFunc+"'"+"/>");
				 	}
				 }
			})
		}
		//增加选题完了以后，去请求查询一个审批的表   需要几个人审批则出现几个审批的框（暂时还没用到） 
		//增加选题
		function applyTopic(){
			var dsId = $('#dsId').val();
			$.ajax({
				type:'post',
				url:'${pageContext.request.contextPath}/topic/approvesTopic',
				data:{'dsId':dsId},
				success:function(data){
					alert("OK"+data.length);
					var str="<div style='border: 1px solid; width: 800px;'>";
					$('#applyTopic').show();
					$('#applyTopic').dialog({
						width:'900',
						collapsible: true,
						minimizable: true,
						maximizable: true
					});
					
					//把列加载到div
					str+="<table><tr><td>审批名称</td><td><input type='text' /></td><td>上级审批</td>"+
							"<td><select><option>是</option>	<option>否</option></select>	</td><td>允许审批</td><td><select>"+
							"<option>是</option>	<option>否</option></select></td></tr><tr><td>审批意见</td><td colspan='5'>"+
							"<textarea rows='2' cols='70'></textarea><input type='button' value='...'/></td></tr>"+
							"<tr><td>状态</td><td><input type='text' value='已通过'/></td><td>审批日期</td><td><input type='text'/></td>"+
							"<td>审批人</td><td><input type='text'/></td></tr></table>"
					str+="</div>";
					var str1="<table><tr><td><input type='button' value='保存' onclick='saveApply()'/></td></tr></table>";
					for(var i=0;i<data.length;i++){
						$('#applyTopic').append(str);
					}
					$('#applyTopic').append(str1);
				}
				
			})
		}
		function saveApply(){
			alert("保存");
			//$.ajax({
				//type:'post',
				//url:'',
				//data:'',
				//success:function(){
					//alert("OK");
				//}
			//})
		}
		
		function confirmation(){
			$('#InsertLink').dialog('close');
			if(parentFlowId == 1){//为1 则默认取节点
				var selRows = $('#LINKDATALIST').datagrid('getSelections');
				for(var i = 0; i < selRows.length; i++){
					$('#PLANDATALIST').datagrid('appendRow',{
						END_DATE:'',
						EXECSTATE:'',
						BEGIN_DATE:'',
						DUTY_BUMEN:'',
						DUTY_MAN:'',
						FLOW_ID:selRows[i].FLOW_ID,
						PARENT_FLOW_ID:'',
						SORT_NUMBER:''
					});
				}
			}else if(parentFlowId == 2){//为2 则添加父节点
				var selParRow = $('#LINKDATALIST').datagrid('getSelected');
				var selRow = $('#PLANDATALIST').datagrid('getSelected');
				if(selRow == null){$.message.alert('提示','请选择一条环节点记录');return;};
				selRow.PARENT_FLOW_ID = selParRow.FLOW_ID+"";
				$('#PLANDATALIST').datagrid('refreshRow', dbRowIndex);
			}
		}
		
		function savePlan(){
			var rows = $('#PLANDATALIST').datagrid('getRows');
			var flowNodes = new Array();
							for(var i = 0; i < rows.length; i++){
								flowNodes[i] = {
								'manageId':rows[i].MANAGE_ID,
								'xtId':xtid,
								'flowId':rows[i].FLOW_ID,
								'parentFlowId':rows[i].PARENT_FLOW_ID,
								'sortNumber':rows[i].SORT_NUMBER,
								'execstate':rows[i].EXECSTATE,
								'inputMan':rows[i].INPUT_MAN,
								'inputDate':rows[i].INPUT_DATE,
								'dutyMan':rows[i].DUTY_MAN,
								'dutyBumen':rows[i].DUTY_BUMEN,
								'beginDate':rows[i].BEGIN_DATE,
								'endDate':rows[i].END_DATE,
								'lastModDate':rows[i].LAST_MOD_DATE,
								'djId':rows[i].DJ_ID,
								'memo':rows[i].MEMO};
							}			
							var flowNodesList = {};
							flowNodesList = flowNodes;
							$.ajax({
				 				url:'${pageContext.request.contextPath}/node/saveFlowNodes',
				 				type:'post',
                  				contentType: "application/json;charset=utf-8",
				 				data:JSON.stringify(flowNodesList),
				 				dataType: "json",
				 				success:function(data){
				 					if(data.msg == 'success'){
				 						$.messager.alert('提示框','保存成功','info');
				 					}else{
				 						$.messager.alert('错误提示框',data.msg,'error');
				 					}
				 					$('#PLANDATALIST').datagrid('reload');
				 				}
				 			});
		}
	</script>
</head>
<body  onresize="resizeGrid();">
	<form action="" method="post" id="queryForm">
		<table cellpadding="0" cellspacing="0" style="width: 100%">
			<tr>
				<td>
					<div style="height:60px;overflow: auto;" >
						<!-- 部分查询框 -->
						<table id="COLUMNS">
						</table>
					</div>
				</td>
				<td style="vertical-align: top;">
					<!-- 部分按钮 -->
					<div id="BUTTONS" style="width:195px;padding-left: 20px;"></div>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<!-- 数据列表 -->	
					<table  id="DATALIST"></table>
				</td>
			</tr>
		</table>
	</form>
	<div id="applyTopic" >
		<table cellpadding="0" cellspacing="0">
			<tr>
				<td>
					<!--录入框 -->
					<table style="padding: 15px;" id="makeColumn">
					</table>
				</td>
			</tr>
			<tr>
				<td style="vertical-align: top; text-align: center;">
					<!-- 部分按钮 -->
					<div id="makeButton" style="padding-left: 20px; text-align: center;"></div>
				</td>
			</tr>
		</table>
	</div>  <!-- 添加按钮点击弹出添加界面 -->
	
	<div id="showPlanW">
		<!-- 显示插入环节 -->
		<div id="showPlanTwo"></div>
	</div>
	<input type="hidden" id="configId" value="${configId}"/>
	<input type="hidden" id="funcsId" value="${funcsId}"/>
	<input type="hidden" id="doubleId" />
</body>
</html>